package com.bwjf.dzfp.common.util;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.codec.binary.Base64;
import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSONObject;
import com.bwjf.dzfp.entity.ResultMsg;

public class StringUtil {
	static Logger logger = Logger.getLogger(StringUtil.class);

	public static String removeNull(String nullStr) {
		if (null == nullStr || "null".equals(nullStr)) {
			return "";
		} else {
			return nullStr;
		}
	}

	/**
	 * 解析二百百货小票中的二维码
	 * 
	 * @param secret
	 *            密文
	 * @return 结果 结果对象中的data属性类型为map，其中包含 xsfNsrsbh,traRecordNum 属性
	 */
	public static ResultMsg parsingURL(String secret) {
		String base64decod = "";
		boolean bo = true;
		try {
			base64decod = new String(Base64.decodeBase64(secret), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			bo = false;
			logger.error("url解析失败,可能格式不符", e);
		}
		if (!bo) {
			return new ResultMsg(false, "解码失败,二维码有误！");
		}
		String[] paras = base64decod.split("&");
		if (paras == null || paras.length != 2) {
			return new ResultMsg(false, "解码失败,二维码有误！");
		}
		String xsfNsrsbh = paras[0].substring(paras[0].indexOf("=") + 1,
				paras[0].length());
		String traRecordNum = paras[1].substring(paras[1].indexOf("=") + 1,
				paras[1].length());
		ResultMsg result = new ResultMsg(true, "成功");
		Map<String, String> map = new HashMap<String, String>();
		map.put("xsfNsrsbh", xsfNsrsbh);
		map.put("traRecordNum", traRecordNum);
		result.setData(map);
		return result;
	}
}
